<?php
include("../../includes/db_connect.php");
ini_set('memory_limit','256M');
mb_detect_order(['ISO-8859-1', 'UTF-8']);
//users
$studentQuery = mysqli_query($db, "SELECT users.id, users.firstname, users.lastname, users.zip, users.pnr, users.address, users.city, users.email, users.phonenumber, users.classId, user_groups.name FROM users LEFT JOIN user_groups ON users.rightsId = user_groups.id WHERE user_groups.name LIKE '%student%'");
$teacherQuery = mysqli_query($db, "SELECT users.id, users.firstname, users.lastname, users.zip, users.pnr, users.address, users.city, users.email, users.phonenumber, users.classId, user_groups.name FROM users LEFT JOIN user_groups ON users.rightsId = user_groups.id WHERE user_groups.name LIKE '%teacher%'");
$totalLength = $studentQuery->num_rows + $teacherQuery->num_rows;
$counter = 0.0;
$content = "";
echo "generating cobolUsers.txt\n";
$first = true;
while(($user = $studentQuery->fetch_assoc())) {
	if(!$first)
		$content .= "\r\n";
	$first = false;
		
	$content .= "E;";
	$content .= html_entity_decode($user["id"]).";";
	$content .= html_entity_decode($user["firstname"])." ".html_entity_decode($user["lastname"]).";";
	$content .= html_entity_decode($user["pnr"]).";";
	$content .= html_entity_decode($user["address"]).";";
	$content .= html_entity_decode($user["zip"]).";";
	$content .= html_entity_decode($user["city"]).";";
	$content .= html_entity_decode($user["phonenumber"]).";";
	$content .= html_entity_decode($user["email"]).";";
	$content .= html_entity_decode($user["classId"]).";";
	++$counter;
	while($counter >= (0.095 * $totalLength)) {
		echo "*";
		$counter -= 0.095 * $totalLength;
	}
}

while(($user = $teacherQuery->fetch_assoc())) {
	$content .= "\r\n";
	$content .= "L;";
	$content .= html_entity_decode($user["id"]).";";
	$content .= html_entity_decode($user["firstname"])." ".html_entity_decode($user["lastname"]).";";
	$content .= html_entity_decode($user["pnr"]).";";
	$content .= html_entity_decode($user["address"]).";";
	$content .= html_entity_decode($user["zip"]).";";
	$content .= html_entity_decode($user["city"]).";";
	$content .= html_entity_decode($user["phonenumber"]).";";
	$content .= html_entity_decode($user["email"]).";";
	$content .= html_entity_decode($user["classId"]).";";
	++$counter;
	while($counter >= (0.095 * $totalLength)) {
		echo "*";
		$counter -= 0.095 * $totalLength;
	}
}
$content = mb_convert_encoding($content, "ISO-8859-1", "UTF-8");
file_put_contents("cobolUsers.txt", mb_convert_encoding($content, "ISO-8859-1"));

//class
$classQuery = mysqli_query($db, "SELECT * FROM class");
$totalLength = $classQuery->num_rows;
$counter = 0.0;
$content = "";
echo "\ngenerating cobolClasses.txt\n";
$content = "";
$first = true;
while(($user = $classQuery->fetch_assoc())) {
	if(!$first)
		$content .= "\r\n";
	$first = false;
		
	$content .= $user["id"].";";
	$content .= $user["name"].$user["year"].";";
	$content .= $user["start"].";";
	$content .= $user["end"].";";
	++$counter;
	while($counter >= (0.095 * $totalLength)) {
		echo "*";
		$counter -= 0.095 * $totalLength;
	}
}
$content = mb_convert_encoding($content, "ISO-8859-1", "UTF-8");
file_put_contents("cobolClasses.txt", $content);

//courses
$coursesQuery = mysqli_query($db, "SELECT * FROM courses");
$totalLength = $coursesQuery->num_rows;
$counter = 0.0;
$content = "";
echo "\ngenerating cobolCourses.txt\n";
$content = "";
$first = true;
while(($user = $coursesQuery->fetch_assoc())) {
	if(!$first)
		$content .= "\r\n";
	$first = false;
		
	$content .= $user["id"].";";
	$content .= $user["name"].";";
	$content .= $user["start"].";";
	$content .= $user["end"].";";
	$content .= $user["classId"].";";
	
	++$counter;
	while($counter >= (0.095 * $totalLength)) {
		echo "*";
		$counter -= 0.095 * $totalLength;
	}
}
$content = mb_convert_encoding($content, "ISO-8859-1", "UTF-8");
file_put_contents("cobolCourses.txt", $content);

//grades
$gradeQuery = mysqli_query($db, "SELECT * FROM grades");
$totalLength = $gradeQuery->num_rows;
$counter = 0.0;
$content = "";
echo "\ngenerating GradesClasses.txt\n";
$content = "";
$first = true;
while(($user = $gradeQuery->fetch_assoc())) {
	if(!$first)
		$content .= "\r\n";
	$first = false;
	$content .= html_entity_decode($user["id"]).";";
	switch(html_entity_decode($user["grade"])) {
		case 0:
			$content .= "IG".";";
			break;
		case 1:
			$content .= "G".";";
			break;
		case 2:
			$content .= "VG".";";
			break;
	}
	$content .= html_entity_decode($user["grade"]).";";
	$content .= html_entity_decode($user["userId"]).";";
	$content .= html_entity_decode($user["courseId"]).";";
	++$counter;
	while($counter >= (0.095 * $totalLength)) {
		echo "*";
		$counter -= 0.095 * $totalLength;
	}
}
$content = mb_convert_encoding($content, "ISO-8859-1", "UTF-8");
file_put_contents("cobolGrades.txt", $content);


?>